Imports OutlookStyleControls
Imports System.ComponentModel

Public Class frmTongHopNhapXuatCacDonVi
    Dim loai_kho As String = ""

    Private Sub frmTongHopNhapXuatCacDonVi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbNhapXuat.Focus()
        dtpTuNgay.Text = "01/01/" + CStr(Date.Now.Year)
        dtpDenNgay.Text = Date.Now.Date.ToString
        rbtSLKH.Checked = True
    End Sub

#Region "txtLoaiKho"
    Private Sub txtLoaiKho_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLoaiKho.DoubleClick
        HienDMLoaiKho()
    End Sub

    Private Sub txtLoaiKho_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLoaiKho.KeyDown
        If (e.KeyCode = Keys.Enter) Or (e.KeyCode = Keys.F1) Then
            HienDMLoaiKho()
        End If
    End Sub

    Public Sub HienDMLoaiKho()
        Dim strDMLoaiKho As String
        strDMLoaiKho = "select KH,TEN from tblDMKhac where L = 'L'"
        Dim ma, ten As String
        ma = ""
        ten = ""
        If (txtLoaiKho.Text <> "") Then
            Dim strSql As String
            strSql = "select TEN from tblDMKhac where L = 'L' and KH = '" & txtLoaiKho.Text & "'"
            Dim ten_loaikho As String
            ten_loaikho = BaseDB.ExecSql_DataValue(strSql)
            If (Not ten_loaikho Is Nothing) Then
                loai_kho = txtLoaiKho.Text
                txtLoaiKho.Text = ten_loaikho
            Else
                'hien DM LoaiKho
                HienDM(strDMLoaiKho, "TEN", "KH", ten, ma)
                loai_kho = ma
                txtLoaiKho.Text = ten
            End If
        Else
            'hien DM LoaiKho
            HienDM(strDMLoaiKho, "TEN", "KH", ten, ma)
            loai_kho = ma
            txtLoaiKho.Text = ten
        End If
    End Sub

    Public Sub HienDM(ByVal strsql As String, ByVal strDM As String, ByVal strVM As String, _
                ByRef DM As String, ByRef VM As String)
        Dim frm As New frmChonDanhMucTheoListBox(strsql, strDM, strVM, "")
        frm.ShowDialog()
        DM = frm.DisplayMember
        VM = frm.ValueMember
    End Sub
#End Region

    Public Function KiemTraNhap() As Boolean
        If (cbbNhapXuat.SelectedIndex = -1) Then
            ShowMessages("Bạn chưa chọn Loại Báo Cáo")
            cbbNhapXuat.Focus()
            Return False
        Else
            If (loai_kho = "") Then
                ShowMessages("Bạn chưa chọn loại kho")
                txtLoaiKho.Focus()
                Return False
            End If
        End If
        Return True
    End Function

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        If (KiemTraNhap()) Then
            Dim strSql As String
            Dim tieude, thoigian, loai_baocao As String
            If (cbbNhapXuat.SelectedIndex = 0) Then 'Nhap
                loai_baocao = "N"
                strSql = "select * from viewTongHopKeHoachNhapXuat where LK = '" & loai_kho & "' and NX = 'N'"
                tieude = "TỔNG HỢP KẾ HOẠCH NHẬP CÁC ĐƠN VỊ"
            Else 'Xuat
                loai_baocao = "X"
                strSql = "select * from viewTongHopKeHoachNhapXuat where LK = '" & loai_kho & "' and NX = 'X'"
                tieude = "TỔNG HỢP KẾ HOẠCH XUẤT CÁC ĐƠN VỊ"
            End If
            Dim tungay, denngay As String
            tungay = CStr(dtpTuNgay.Value.Month) + "/" + CStr(dtpTuNgay.Value.Day) + "/" + CStr(dtpTuNgay.Value.Year)
            denngay = CStr(dtpDenNgay.Value.Month) + "/" + CStr(dtpDenNgay.Value.Day) + "/" + CStr(dtpDenNgay.Value.Year)
            strSql &= " and (NGAY >= '" + tungay + "' and NGAY <= '" + denngay + "')"
            thoigian = "Từ  " + dtpTuNgay.Text + " Đến " + dtpDenNgay.Text
            Dim ds As DataSet
            ds = BaseDB.ExecSql_DataSet(strSql)
            Dim nnreport As New rptBaoCaoTongHopNhapXuatTheoCacDonVi
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue(0, tieude)
            nnreport.SetParameterValue(1, thoigian)
            nnreport.SetParameterValue(2, txtLoaiKho.Text)
            If (rbtSLKH.Checked) Then
                nnreport.SetParameterValue(3, "SLKH")
            Else 'SLTH
                nnreport.SetParameterValue(3, "SLTH")
            End If
            nnreport.SetParameterValue("loai_baocao", loai_baocao)
            Dim frm As New frmReportViewer(nnreport)
            frm.ShowDialog()
        End If
    End Sub

End Class